﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading;

namespace BPMS.Utility
{
    public class RandomHelper
    {
        public static bool NextBoolean()
        {
            Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
            return random.Next(byte.MinValue, byte.MaxValue) > (byte.MaxValue / 2);
        }

        public static Color NextColor()
        {
            Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
            return Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
        }

        public static Color NextColorWithAlpha()
        {
            Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
            return Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
        }

        public static DateTime NextDateTime()
        {
            return NextDateTime(DateTime.MinValue.Year, DateTime.MaxValue.Year);
        }

        public static DateTime NextDateTime(int minYear, int maxYear)
        {
            Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
            int year = random.Next(minYear, maxYear);
            int month = random.Next(1, 12);
            int day = random.Next(1, 28);
            int hour = random.Next(0, 23);
            int minute = random.Next(0, 59);
            int second = random.Next(0, 59);

            return new DateTime(year, month, day, hour, minute, second);
        }

        public static DateTime NextDateTime(int minYear, int maxYear, int minMonth, int maxMonth,
                                            int minDay, int maxDay)
        {
            Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
            int year = random.Next(minYear, maxYear);
            int month = random.Next(minMonth, maxMonth);
            int day = random.Next(minDay, maxDay);
            int hour = random.Next(0, 23);
            int minute = random.Next(0, 59);
            int second = random.Next(0, 59);

            return new DateTime(year, month, day, hour, minute, second);
        }

        public static T NextItem<T>(IEnumerable<T> enumerable)
        {
            Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
            return enumerable.ElementAt(random.Next(0, enumerable.Count() - 1));
        }

        /// <summary>
        /// 对一个数组进行随机排序
        /// </summary>
        /// <typeparam name="T">数组的类型</typeparam>
        /// <param name="arr">需要随机排序的数组</param>
        public static void NextArray<T>(T[] arr)
        {
            //对数组进行随机排序的算法:随机选择两个位置，将两个位置上的值交换

            //交换的次数,这里使用数组的长度作为交换次数
            int count = arr.Length;

            //开始交换
            for (int i = 0; i < count; i++)
            {
                //生成两个随机数位置
                int randomNum1 = NextInt(0, arr.Length);
                int randomNum2 = NextInt(0, arr.Length);

                //定义临时变量
                T temp;

                //交换两个随机数位置的值
                temp = arr[randomNum1];
                arr[randomNum1] = arr[randomNum2];
                arr[randomNum2] = temp;
            }
        }

        /// <summary>
        /// 生成一个指定范围的随机整数，该随机数范围包括最小值，但不包括最大值
        /// </summary>
        /// <param name="minNum">最小值</param>
        /// <param name="maxNum">最大值</param>
        public static int NextInt(int minNum, int maxNum)
        {
            Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
            return random.Next(minNum, maxNum);
        }

        /// <summary>
        /// 获取从0开始的随机数字
        /// </summary>
        /// <param name="maxNum"></param>
        /// <returns></returns>
        public static int NextInt(int maxNum)
        {
            Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
            return random.Next(0, maxNum);
        }

        /// <summary>
        /// 获取非负随机数
        /// </summary>
        /// <returns></returns>
        public static int Next()
        {
            Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
            return random.Next();
        }

        /// <summary>
        /// 生成一个0.0到1.0的随机小数
        /// </summary>
        public static double NextRandomDouble()
        {
            Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
            return random.NextDouble();
        }

        /// <summary>
        /// 随机生成指定长度的字母和数字
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string NextAlphaNumeric(byte length)
        {
            int n = AlphaNumericString.Length;
            string result = string.Empty;
            System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
            for (int i = 0; i < length; i++)
            {
                int rnd = random.Next(0, n);
                result += AlphaNumericString[rnd];
            }
            return result;
        }

        const string AlphaNumericString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    }
}
